﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.Slider;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolSound extends MovieClip
	{
		public static const NAME:String = "Tool-Sound";

		private var overLay:OverLay;
		
		private var musicSlider:Slider;
		private var effectSlider:Slider;
		private var voiceSlider:Slider;
		
		public function ToolSound()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			musicSlider = new Slider();
			musicSlider.x = 80;
			musicSlider.y = 80;
			musicSlider.width = 240;
			musicSlider.height = 3;
			musicSlider.maximum = 100;
			musicSlider.minimum = 0;
			musicSlider.snapInterval = 10;
			musicSlider.tickInterval = 10;
			musicSlider.enabled = false;
			this.addChild(musicSlider);
			
			effectSlider = new Slider();
			effectSlider.x = 80;
			effectSlider.y = 150;
			effectSlider.width = 240;
			effectSlider.height = 3;
			effectSlider.maximum = 100;
			effectSlider.minimum = 0;
			effectSlider.snapInterval = 10;
			effectSlider.tickInterval = 10;
			effectSlider.enabled = false;
			this.addChild(effectSlider);
			
			voiceSlider = new Slider();
			voiceSlider.x = 80;
			voiceSlider.y = 220;
			voiceSlider.width = 240;
			voiceSlider.height = 3;
			voiceSlider.maximum = 100;
			voiceSlider.minimum = 0;
			voiceSlider.snapInterval = 10;
			voiceSlider.tickInterval = 10;
			voiceSlider.enabled = false;
			this.addChild(voiceSlider);
			
			btnMusicMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnMusicPlus.addEventListener(MouseEvent.CLICK, onClick);
			btnEffectMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnEffectPlus.addEventListener(MouseEvent.CLICK, onClick);
			btnVoiceMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnVoicePlus.addEventListener(MouseEvent.CLICK, onClick);
			
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnSave.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(400, 300);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setMusic":
					musicSlider.value = params.Volume;
					
					if (musicSlider.value == 0)
					{
						btnMusicMinus.visible = false;
					}
					
					if (musicSlider.value == 100)
					{
						btnMusicPlus.visible = false;
					}
					break;
				case "setEffect":
					effectSlider.value = params.Volume;
					
					if (effectSlider.value == 0)
					{
						btnEffectMinus.visible = false;
					}
					
					if (effectSlider.value == 100)
					{
						btnEffectPlus.visible = false;
					}
					break;
				case "setVoice":
					voiceSlider.value = params.Volume;
					
					if (voiceSlider.value == 0)
					{
						btnVoiceMinus.visible = false;
					}
					
					if (voiceSlider.value == 100)
					{
						btnVoicePlus.visible = false;
					}
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
		
			btnMusicMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnMusicPlus.removeEventListener(MouseEvent.CLICK, onClick);
			btnEffectMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnEffectPlus.removeEventListener(MouseEvent.CLICK, onClick);
			btnVoiceMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnVoicePlus.removeEventListener(MouseEvent.CLICK, onClick);
			
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnSave.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{			
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectSlider.value * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolSound"]));
					break;
				case "btnSave":
					dispatchEvent(new SwcEvent(NAME, ["-saveVolume", musicSlider.value + ":" + effectSlider.value + ":" + voiceSlider.value]));
					break;
				case "btnMusicMinus":
					musicSlider.value -= 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Music:" + musicSlider.value]));
					
					if (musicSlider.value > 0)
					{						
						btnMusicPlus.visible = true;
					}
					else
					{
						btnMusicMinus.visible = false;
					}
					break;
				case "btnMusicPlus":
					musicSlider.value += 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Music:" + musicSlider.value]));
					
					if (musicSlider.value < 100)
					{
						btnMusicMinus.visible = true;
					}
					else
					{
						btnMusicPlus.visible = false;
					}
					break;
				case "btnEffectMinus":
					effectSlider.value -= 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Effect:" + effectSlider.value]));
					
					if (effectSlider.value > 0)
					{						
						btnEffectPlus.visible = true;						
					}
					else
					{
						btnEffectMinus.visible = false;
					}
					break;
				case "btnEffectPlus":
					effectSlider.value += 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Effect:" + effectSlider.value]));
					
					if (effectSlider.value < 100)
					{
						btnEffectMinus.visible = true;						
					}
					else
					{
						btnEffectPlus.visible = false;
					}
					break;
				case "btnVoiceMinus":
					voiceSlider.value -= 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Voice:" + voiceSlider.value]));
					
					if (voiceSlider.value > 0)
					{
						btnVoicePlus.visible = true;						
					}
					else
					{
						btnVoiceMinus.visible = false;
					}
					break;
				case "btnVoicePlus":
					voiceSlider.value += 10;
					dispatchEvent(new SwcEvent(NAME, ["-setVolume", "Voice:" + voiceSlider.value]));
					
					if (voiceSlider.value < 100)
					{						
						btnVoiceMinus.visible = true;						
					}
					else
					{
						btnVoicePlus.visible = false;
					}
					break;
			}
		}
	}
}